package com.bw.ivideoproject.home.detailFragment

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.SPUtils
import com.bw.ivideoproject.R
import com.bw.ivideoproject.databinding.FragmentCommentBinding
import com.bw.ivideoproject.databinding.ItemCommentBinding
import com.bw.ivideoproject.entity.CommentEntity
import com.bw.ivideoproject.entity.SimpleVideoEntity
import com.bw.ivideoproject.home.HomeViewModel
import com.bw.ivideoproject.login.LoginActivity
import com.bw.lib_imageutils.ImageLoaderManager
import com.bw.lib_imageutils.ImageOptions
import com.bw.lib_mvvm.BaseFragment


class CommentFragment : BaseFragment<FragmentCommentBinding>() {

    private val viewModel:HomeViewModel by lazy {
        ViewModelProvider(this)[HomeViewModel::class.java]
    }
    private lateinit var commentAdapter: CommentAdapter

    private lateinit var video:SimpleVideoEntity

    override fun bindLayout(): Int {
        return R.layout.fragment_comment
    }

    override fun initView() {
        video = arguments?.getSerializable("video") as SimpleVideoEntity
        commentAdapter = CommentAdapter()
        binding.rvHomeDetailCommentComments.apply {
            this.adapter = commentAdapter
            this.layoutManager = LinearLayoutManager(requireContext())
        }

    }

    override fun initData() {
        viewModel.getComment(0,video.item_id)
        binding.btnHomeDetailCommentPublish.setOnClickListener {
            val msg = binding.etHomeDetailCommentContent.text.toString()
            val id = SPUtils.getInstance().getInt("id")
            val icon = SPUtils.getInstance().getString("icon")
            if(msg.isNullOrEmpty()){
                Toast.makeText(requireContext(),"评论内容不能为空",Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
//            if(icon.isNullOrEmpty()){
//                Toast.makeText(requireContext(),"未登录",Toast.LENGTH_SHORT).show()
//                startActivity(Intent(requireActivity(),LoginActivity::class.java))
//                return@setOnClickListener
//            }
            val comment = CommentEntity(0,msg,"",
                0,0,video.item_id,"匿名用户",
                arrayListOf(),0,id,icon)
            viewModel.sendComment(comment)
        }
    }

    override fun handleObserver() {
        super.handleObserver()
        viewModel.filedLiveData.observe(this){
            Toast.makeText(requireContext(),it,Toast.LENGTH_SHORT).show()
        }
        viewModel.commentLiveData.observe(this){
            commentAdapter.refresh(it)
        }
        viewModel.sendLiveData.observe(this){
            commentAdapter.addComment(it)
        }
    }

    inner class CommentAdapter() : RecyclerView.Adapter<CommentAdapter.ViewHolder>() {

        private var data = mutableListOf<CommentEntity>()

        inner class ViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){
            val subBinding = ItemCommentBinding.bind(itemView)
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val inflate = LayoutInflater.from(parent.context).inflate(R.layout.item_comment, parent, false)
            return ViewHolder(inflate)
        }

        override fun getItemCount(): Int {
            return data.size
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val entity = data[position]
            val option = ImageOptions.Builder().setPlaceImage(R.mipmap.ic_launcher).build()
            ImageLoaderManager.getInstance().loadFileIntoImageView(requireContext(),entity.userlogo,holder.subBinding.homeDetailCommentHeaderimage,option)
            holder.subBinding.homeDetailCommentUsername.text = entity.nickname.ifBlank { "匿名用户" }
            holder.subBinding.homeDetailCommentContent.text = entity.content
            holder.subBinding.homeDetailCommentAgreeCount.text = entity.agreenum.toString()
        }

        fun refresh(list :List<CommentEntity>){
            data.addAll(list)
            this.notifyDataSetChanged()
        }

        fun addComment(comment:CommentEntity){
            data.add(0,comment)
            this.notifyItemChanged(0)
        }
    }

}